package io

import (
	"jvmgo/native"
	"jvmgo/rtda"
	"os"
	"unsafe"
)

func init() {
	native.Register("java/io/FileOutputStream", "writeBytes", "([BIIZ)V", writeBytes)
}

func writeBytes(frame *rtda.Frame) {
	vars := frame.LocalVars()
	b := vars.GetRef(1)
	off := vars.GetInt(2)
	len := vars.GetInt(3)
	jBytes := b.Data().([]int8)
	goBytes := castInt8sToUint8s(jBytes)
	goBytes = goBytes[off : off+len]
	os.Stdout.Write(goBytes)
}

func castInt8sToUint8s(bytes []int8) []byte {
	ptr := unsafe.Pointer(&bytes)
	return *((*[]byte)(ptr))

}
